#添加一个Mixin扩展类:帮助我们判断用户是否登录
from django import http
from django.contrib.auth.decorators import login_required

#导入：
from django.utils.decorators import  wraps

from meiduo_mall.utils.response_code import RETCODE


def login_required_json(view_func):
    '''
    判断用户是否登录的装饰器，并返回json
    :param view_func: 被装饰的视图函数
    :return: json、view_func
    '''

    #恢复view_func的名字和文档
    @wraps(view_func)
    def wrapper(request,*args,**kwargs):
        #如果用户登录，返回json数据
        if not request.user.is_authenticated():
            return http.JsonResponse({'code':RETCODE.SESSIONERR,'errmsg':'用户未登录'})
        else:
        #如果用户登录，进入到view_func中
            return view_func(request,*args,**kwargs)

    return wrapper


class LoginRequiredJSONMixin(object):
    '''验证用户是否登录并返回扩展类'''
    @classmethod
    def as_view(cls,**initkwargs):
        view=super().as_view(**initkwargs)
        return login_required_json(view)

class LoginRequiredMinxin(object):
   #重写as_view
   @classmethod
   def as_view(cls,**initkwargs):
       view=super().as_view()
       return login_required(view)